<template>
  <el-upload
    action=""
    list-type="picture-card"
    :before-upload="beforeAvatarUpload"
    :http-request="upLoadImage"
  >
    <i
      slot="default"
      class="el-icon-plus"
    ></i>
    <div
      slot="file"
      slot-scope="{file}"
    >
      <span class="el-upload-list__item-actions">
        <span
          class="el-upload-list__item-preview"
          @click="handlePictureCardPreview(file)"
        >
          <i class="el-icon-zoom-in"></i>
        </span>
        <span
          v-if="!disabled"
          class="el-upload-list__item-delete"
          @click="handleRemove(file)"
        >
          <i class="el-icon-delete"></i>
        </span>
      </span>
    </div>
  </el-upload>
</template>

<script>
import COS from 'cos-js-sdk-v5'
export default {
  name: 'UploadImage',
  props: {
    test: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      // newImgList
    }
  },
  methods: {
  // 上传前文件校验
    beforeAvatarUpload(file) {
      const fileTypeList = ['image/jpeg', 'iamge/bmp', 'image/png']
      const isLt5M = file.size / 1024 / 1024 < 5
      if (!isLt5M) {
        this.$message.error('不支持当前文件格式')
      }
      if (fileTypeList.includes(file.type)) {
        // 发送请求
        this.upLoadImage(file)
      } else {
        this.$message.error('上传头像图片只能是 JPG PNG GIF BMP 格式!')
      }
    },
    async upLoadImage(params) {
      const cos = new COS({
        SecretId: 'AKID2tZ1iJi4KNGwOcdgSyFTM7dvAJEvQsAE',
        SecretKey: 'wvmcVItJYfaYbt22CEC76RdgWI95ubCo'
      })
      await cos.putObject(
        {
          Bucket: 'shenlingwuliu-1318882557', // 存储桶名称
          Region: 'ap-beijing', // 地域名称
          Key: params.name, // 文件名称
          StorageClass: 'STANDARD', // 固定值
          Body: params // 文件对象
        },
        (err, data) => {
          if (data.statusCode === 200 && data.Location) {
            // 拿到了腾讯云返回的地址
            console.log(this.driversLicenseInfo.picture)
            this.newImgList.push('http://' + data.Location)
            this.imgUrlList = this.newImgList
            this.driversLicenseInfo.picture = this.imgUrlList.toString()
          } else {
            this.$message.error(err.Message) // 上传失败提示消息
          }
        }
      )
    }
  }
}
</script>

<style>

</style>
